package com.kws.merchant.service.impl;


import com.kws.common.core.utils.StringUtils;
import com.kws.common.security.auth.AuthUtil;
import com.kws.merchant.domain.ItemSchemeMerchant;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kws.merchant.domain.OperationPolling;
import com.kws.merchant.domain.po.ItemPo;
import com.kws.merchant.mapper.ItemSchemeMerchantMapper;
import com.kws.merchant.service.ItemSchemeMerchantService;
import com.kws.merchant.util.TimeUtil;
import com.kws.system.api.domain.SysUser;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.util.Date;
import java.util.List;

/**
 * 报价-项目商家关联表(ItemSchemeMerchant)表服务实现类
 *
 * @author yxy
 * @since 2024-11-12 09:45:59
 */
@Service("itemSchemeMerchantService")
public class ItemSchemeMerchantServiceImpl extends ServiceImpl<ItemSchemeMerchantMapper, ItemSchemeMerchant> implements ItemSchemeMerchantService {

    @Resource
    private ItemSchemeMerchantMapper itemSchemeMerchantMapper;


    /**
     * 分页查询列表
     */
    @Override
    public Page<ItemSchemeMerchant> pageList(ItemPo itemPo) {

        Integer pageSize = itemPo.getPageSize();
        Integer totalPage = itemPo.getTotalPage();
        String itemName = itemPo.getName();//方案名称
        SysUser sysUser = AuthUtil.getLoginUser().getSysUser();
        String sql=" o.merchant_id="+sysUser.getCompanyId();
        if (StringUtils.isNotEmpty(itemName)){
            sql+=" and s.scheme_name like '%"+itemName+"%' ";
        }
        int sizeNum = (pageSize - 1) * totalPage;//计算分页
        int total = itemSchemeMerchantMapper.countToTal(sql);
        //查询签名数据
        List<ItemSchemeMerchant> records=itemSchemeMerchantMapper.getRecords(sql,sizeNum,totalPage);
        Date date = new Date();
        records.forEach(x->{
            int days = TimeUtil.getDays(date, x.getDeadlineTime());
            x.setIndate(days+"天");

        });
        Page<ItemSchemeMerchant> page = new Page<>();
        page.setRecords(records);
        page.setTotal(total);
        page.setCurrent(pageSize);
        page.setPages((total+totalPage-1)/totalPage);
        page.setSize(totalPage);
        return page;
    }

    @Override
    public List<ItemSchemeMerchant> detailScheme() {
        SysUser sysUser = AuthUtil.getLoginUser().getSysUser();
        Long companyId = sysUser.getCompanyId();
        return this.list(Wrappers.<ItemSchemeMerchant>lambdaQuery().eq(ItemSchemeMerchant::getMerchantId,companyId));
    }


}
